<script setup>
import {onMounted, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {DeleteE, ListE, SaveE, SaveW} from "@/utils/api/admin.js";

const data = ref([{
  eno: '',
  enterpriseName: '',
  registerNumber: '',
  legalRepresentative: '',
  password: '',
}]);
const seEno = ref();
const visible = ref(false);
const enterprise = ref({
  eno: '',
  enterpriseName: '',
  registerNumber: '',
  legalRepresentative: '',
  password: '',
})

const list = () => {
  data.value = [];
  ListE(seEno.value).then(rs => {
    if (rs.code === 1) {
      data.value = rs.data;
    } else {
      ElMessage({
        message: '服务器错误',
        type: 'error',
      })
    }
  }).catch(error => console.error(error.toString()));
}

const openSave = () => {
  visible.value = true;
  enterprise.value = {
    eno: '',
    enterpriseName: '',
    registerNumber: '',
    legalRepresentative: '',
    password: '',
  }
}

const save = () => {
  const e = enterprise.value;
  if (e.eno === '' || e.enterpriseName === '' || e.registerNumber === '' ||
      e.legalRepresentative === '' || e.password === '') {
    ElMessage({
      message: '输入不能为空',
      type: 'error',
    });
    return;
  }
  SaveE(e.eno, e.enterpriseName, e.registerNumber,
      e.legalRepresentative, e.password).then(rs => {
    if (rs.code === 1) {
      ElMessage({
        message: '添加企业成功',
        type: 'success',
      })
      setTimeout(list,100)
    } else {
      ElMessage({
        message: '添加企业失败，请重试',
        type: 'warning',
      })
    }
    enterprise.value = {};
  }).catch(error => console.error(error.toString()));
  visible.value = false;
}

const deleteE = (index) => {
  ElMessageBox({
    message: '确定删除该企业',
    type: 'warning',
  }).then(() => {
    DeleteE(data.value[index].eno).then(rs => {
      if (rs.code === 1) {
        data.value.splice(index, 1);
        ElMessage({
          message: '删除成功',
          type: 'success',
        })
      } else {
        ElMessage({
          message: '删除失败，请重试',
          type: 'warning',
        })
      }
    }).catch(error => console.error(error.toString()));
  }).catch(() => {
  });
}

onMounted(() => {
  list();
})
</script>

<template>
  <el-form label-width="100px" inline>
    <el-form-item label="企业编号">
      <el-input v-model="seEno"/>
    </el-form-item>
    <el-form-item>
      <el-button @click="list" type="primary">搜 索</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="data" border stripe max-height="560px">
    <el-table-column prop="eno" label="企业编号"/>
    <el-table-column prop="enterpriseName" label="企业名称"/>
    <el-table-column prop="registerNumber" label="企业注册号"/>
    <el-table-column prop="legalRepresentative" label="法人代表"/>
    <el-table-column prop="password" label="企业密码"/>
    <el-table-column label="操作">
      <template #header>
        <el-button type="primary" link @click="openSave">添加企业</el-button>
      </template>
      <template #default="scope">
        <el-button type="danger" link @click="deleteE(scope.$index)">删 除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog v-model="visible" width="500px" align-center draggable @close="enterprise = {};"
             title="新增企业">
    <el-form label-width="80px">
      <el-form-item label="企业编号">
        <el-input v-model="enterprise.eno"/>
      </el-form-item>
      <el-form-item label="企业名称">
        <el-input v-model="enterprise.enterpriseName"/>
      </el-form-item>
      <el-form-item label="注册编号">
        <el-input v-model="enterprise.registerNumber"/>
      </el-form-item>
      <el-form-item label="法人代表">
        <el-input v-model="enterprise.legalRepresentative"/>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="enterprise.password"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="save">保 存</el-button>
    </template>
  </el-dialog>

</template>
<style scoped>

</style>